﻿Imports System.IO
Imports System.Xml.Serialization
Imports Ionic.Zip

Public Class ProfileManager

    Public Shared Sub CreateNewProfile(nf As NewProfileForm)
        Dim user = nf.Profile.UserName
        Dim appdata = FileSystemUtilities.AppPath

        Dim profile = nf.Profile
        profile.KeyLength = nf.Keylength

        'Generating RSA keys
        Dim Sigkeypair = SharedTools.KeysAndDigestsUtilities.GenerateRSAKeypair(nf.Keylength)
        Dim Cryptokeypair = SharedTools.KeysAndDigestsUtilities.GenerateRSAKeypair(nf.Keylength)

        profile.SigCert = New CertificateItem
        profile.CryptoCert = New CertificateItem

        profile.SigCert.CertificateKey(nf.Password) = Sigkeypair
        profile.CryptoCert.CertificateKey(nf.Password) = Cryptokeypair

        SaveProfile(profile, nf.Password)

        Repository.CurrentPassword = nf.Password
        Repository.CurrentProfile = profile
    End Sub

    Public Shared Function LoadProfile(user As String, pass As String) As Boolean
        Dim appdata = FileSystemUtilities.AppPath
        Dim xmlProfile As String = String.Empty

        Try
            Repository.CurrentPassword = pass
            Using zip1 As ZipFile = ZipFile.Read(appdata & "\ProgettoSicurezza\users\" & user & ".zip")
                zip1.Password = pass
                For Each item In zip1
                    If item.FileName = "profile.xml" Then
                        item.Password = pass
                        Dim stream As New IO.MemoryStream
                        item.Extract(stream)
                        stream.Position = 0
                        Using sr As New StreamReader(stream)
                            xmlProfile = sr.ReadToEnd
                        End Using
                        stream.Close()
                    End If
                Next
            End Using

            'carica profilo
            Dim s As New XmlSerializer(GetType(ClientProfile))
            Dim xr As New IO.StringReader(xmlProfile)
            Dim profile = DirectCast(s.Deserialize(xr), ClientProfile)
            xr.Close()

            Repository.CurrentProfile = profile

            Return True

        Catch ex As BadPasswordException
            'Estrazione fallita, password non valida
            Repository.CurrentPassword = ""
            Return False
        End Try
    End Function


    Public Shared Sub SaveProfile(profile As ClientProfile, pass As String)
        FileSystemUtilities.CheckAndCreateDirectories()

        Dim appdata = FileSystemUtilities.AppPath
        If File.Exists(appdata & "\ProgettoSicurezza\users\" & profile.UserName & ".zip") Then
            File.Delete(appdata & "\ProgettoSicurezza\users\" & profile.UserName & ".zip")
        End If


        Dim s As New XmlSerializer(GetType(ClientProfile))
        Dim sw As New StringWriter()
        s.Serialize(sw, profile)

        Dim zip As New ZipFile(appdata & "\ProgettoSicurezza\users\" & profile.UserName & ".zip")
        zip.Password = pass
        zip.Encryption = EncryptionAlgorithm.WinZipAes256
        zip.AddEntry("profile.xml", sw.ToString, Text.Encoding.Unicode)
        zip.Save()
        sw.Close()
    End Sub

    Public Shared Sub SaveProfile(profile As ClientProfile)
        SaveProfile(profile, Repository.CurrentPassword)
    End Sub
End Class
